//
//  YWTheHotestAnchorTableViewCell.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWTheHotestAnchorTableViewCell.h"
#import "LTDataBase.h"
#import "LTAnchorModel.h"

@implementation YWTheHotestAnchorTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.lineLabel = [[UILabel alloc] init];
        _lineLabel.backgroundColor = [UIColor colorWithWhite:0.886 alpha:1.000];
        [self.contentView addSubview:_lineLabel];
        
        self.photoImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_photoImageView];
        
        self.nicknameLabel = [[UILabel alloc] init];
        _nicknameLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_nicknameLabel];
        
        self.vImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_vImageView];
        
        self.personDescribeLabel = [[UILabel alloc] init];
        _personDescribeLabel.font = [UIFont systemFontOfSize:13];
        _personDescribeLabel.textColor = [UIColor colorWithWhite:0.588 alpha:1.000];
        [self.contentView addSubview:_personDescribeLabel];
        
        self.musicImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_musicImageView];
        
        self.tracksCountsLabel = [[UILabel alloc] init];
        _tracksCountsLabel.font = [UIFont systemFontOfSize:13];
        _tracksCountsLabel.textColor = [UIColor colorWithWhite:0.588 alpha:1.000];
        [self.contentView addSubview:_tracksCountsLabel];
        
        self.doubleHeadImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_doubleHeadImageView];
        
        self.followersDescribeLabel = [[UILabel alloc] init];
        _followersDescribeLabel.font = [UIFont systemFontOfSize:13];
        _followersDescribeLabel.textColor = [UIColor colorWithWhite:0.588 alpha:1.000];
        [self.contentView addSubview:_followersDescribeLabel];
        
        self.playImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_playImageView];
        
        self.listenLabel = [[UILabel alloc] init];
        _listenLabel.font = [UIFont systemFontOfSize:13];
        _listenLabel.textColor = [UIColor colorWithWhite:0.588 alpha:1.000];
        [self.contentView addSubview:_listenLabel];
        
        self.attentionImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_attentionImageView];
        self.attentionImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickAttentionImageView)];
        [self.attentionImageView addGestureRecognizer:tap];
        
        [self addObserver:self forKeyPath:@"isDid" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return self;
}


- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"isDid"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"isDid"]) {
        BOOL isDid = [[change objectForKey:@"new"] boolValue];
        if (isDid) {
            _attentionImageView.image = [UIImage imageNamed:@"find_radio_focuse_sel"];
        }
        else
        {
            _attentionImageView.image = [UIImage imageNamed:@"find_radio_focuse"];
        }
    }
}

- (void)didClickAttentionImageView
{
    LTDataBase *data = [LTDataBase shareLTDataBaseTool];
    
    if (_isDid == NO) {
        [MBProgressHUD showSuccess:@"已关注"];
        LTAnchorModel *model = [[LTAnchorModel alloc] init];
        model.uid = self.hotestModel.uid;
        model.title = self.hotestModel.personDescribe;
        model.smallLogo = self.hotestModel.largeLogo;
        model.nickname = self.hotestModel.nickname;
        [data insertDataWithAnchorModel:model];
        self.isDid = YES;
        
    }
    else
    {
        [MBProgressHUD showSuccess:@"已取消关注"];
        [data delegateDataWithModel_id:self.hotestModel.uid type:3];
        self.isDid = NO;
    }
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    _lineLabel.frame = CGRectMake(0, 0, SCREENWIDTH, 5 * FITWIDTH);
    _photoImageView.frame = CGRectMake(0, 5 * FITHEIGHT, 145 * FITWIDTH, 145 * FITHEIGHT);
    _nicknameLabel.frame = CGRectMake(_photoImageView.bounds.size.width + 10 * FITWIDTH, 30 * FITHEIGHT, 100 * FITWIDTH, 30 * FITHEIGHT);
    [_nicknameLabel sizeToFit];
    _vImageView.frame = CGRectMake(_nicknameLabel.bounds.size.width + _nicknameLabel.frame.origin.x + 10 * FITWIDTH, _nicknameLabel.frame.origin.y, 18 * FITWIDTH, 18 * FITWIDTH);
    NSString *string = _hotestModel.personDescribe;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13], NSFontAttributeName, nil];
    CGRect newRect = [string boundingRectWithSize:CGSizeMake(SCREENWIDTH - _nicknameLabel.frame.origin.x - 20 * FITWIDTH, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    _personDescribeLabel.frame = CGRectMake(_nicknameLabel.frame.origin.x, _nicknameLabel.frame.origin.y + _nicknameLabel.bounds.size.height + 10 * FITHEIGHT, SCREENWIDTH - _nicknameLabel.frame.origin.x - 20 * FITWIDTH, newRect.size.height);
    _musicImageView.frame = CGRectMake(_nicknameLabel.frame.origin.x, _personDescribeLabel.frame.origin.y + _personDescribeLabel.bounds.size.height + 10 * FITHEIGHT, 13 * FITWIDTH, 13 * FITWIDTH);
    _tracksCountsLabel.frame = CGRectMake(_musicImageView.frame.origin.x + 20 * FITWIDTH, _musicImageView.frame.origin.y - 2 * FITHEIGHT, 100, 20 * FITHEIGHT);
    [_tracksCountsLabel sizeToFit];
    _doubleHeadImageView.frame = CGRectMake(_tracksCountsLabel.frame.origin.x + _tracksCountsLabel.bounds.size.width + 5 * FITWIDTH, _tracksCountsLabel.frame.origin.y + 2 * FITHEIGHT, 17 * FITWIDTH, 12 * FITWIDTH);
    _followersDescribeLabel.frame = CGRectMake(_doubleHeadImageView.frame.origin.x + 20 * FITWIDTH, _doubleHeadImageView.frame.origin.y - 2 * FITHEIGHT, 100 * FITWIDTH, 20 * FITHEIGHT);
    [_followersDescribeLabel sizeToFit];
    _playImageView.frame = CGRectMake(_photoImageView.bounds.size.width + 10 * FITWIDTH, self.bounds.size.height - 30 * FITHEIGHT, 20 * FITWIDTH, 20 * FITWIDTH);
    _listenLabel.frame = CGRectMake(_playImageView.frame.origin.x + 28 * FITWIDTH, _playImageView.frame.origin.y + 2 * FITHEIGHT, 20 * FITWIDTH, 15 * FITHEIGHT);
    [_listenLabel sizeToFit];
    _attentionImageView.frame = CGRectMake(315 * FITWIDTH, 120 * FITHEIGHT, 50 * FITWIDTH, 22 * FITHEIGHT);
}

- (void)setHotestModel:(YWTheHotestModel *)hotestModel
{
    if (_hotestModel != hotestModel) {
        _hotestModel = hotestModel;
        
    }
    [_photoImageView sd_setImageWithURL:[NSURL URLWithString:hotestModel.largeLogo] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
    _nicknameLabel.text = hotestModel.nickname;
    _vImageView.image = [UIImage imageNamed:@"V"];
    _personDescribeLabel.text = hotestModel.personDescribe;
    _musicImageView.image = [UIImage imageNamed:@"album_tracks"];
    _tracksCountsLabel.text = [NSString stringWithFormat:@"%ld",hotestModel.tracksCounts];
    _doubleHeadImageView.image = [UIImage imageNamed:@"双人像"];
    CGFloat num = hotestModel.followersCounts;
    if (num > 10000) {
        _followersDescribeLabel.text = [NSString stringWithFormat:@"%.1f万", num / 10000];
    }
    else
    {
        _followersDescribeLabel.text = [NSString stringWithFormat:@"%.f", num];
    }
    _playImageView.image = [UIImage imageNamed:@"liveRadioCellPlay"];
    _listenLabel.text = @"试听";
    LTDataBase *data = [LTDataBase shareLTDataBaseTool];
    NSInteger tempNum = [data isSaveDataInTableWithModel_id:hotestModel.uid type:3];
    if (tempNum == 0) {
        self.isDid = YES;
    }
    else
    {
        self.isDid = NO;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
